package com.cskaoyan.service.topic.wx;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.coupon.wx.ListData;
import com.cskaoyan.bean.topic.TopicDetailWithCommentsVO;
import com.cskaoyan.mapper.CommentMapper;
import com.cskaoyan.mapper.TopicMapper;
import com.cskaoyan.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class TopicService2Impl implements TopicService2 {

    @Autowired
    TopicMapper topicMapper;
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    UserMapper userMapper;


    @Override
    public Topic detail(String id) {
        Topic topic = topicMapper.selectByPrimaryKey(Integer.valueOf(id));
        return topic;
    }


    @Override
    public List<Topic> related(String id) {
        TopicExample topicExample = new TopicExample();
        List<Topic> list = topicMapper.selectByExampleWithBLOBs(topicExample);
        //为list排序，若 read count 带k，要先处理
        for (Topic topic : list) {
            String readCount = topic.getReadCount();
            if (readCount.endsWith("k")){
                String k = readCount.replace("k", "00").replace(".", "");
                topic.setReadCount(k);
            }
        }
        //比较器向从大到小（逆序）应该用o2的值减o1的值
        Collections.sort(list, new Comparator<Topic>() {
            @Override
            public int compare(Topic o1, Topic o2) {
                return Integer.valueOf(o2.getReadCount()) - Integer.valueOf(o1.getReadCount());
            }
        });

        List<Topic> list1 = new LinkedList<>();
        for (int i = 0; i < 4; i++) {
            try {
                list1.add(list.get(i));
            }catch (IndexOutOfBoundsException e){
                //如果专题数不足四条，捕捉此异常
                e.printStackTrace();
            }
        }
        return list1;
    }

    @Override
    public ListData<Topic> list(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        TopicExample topicExample = new TopicExample();
        TopicExample.Criteria criteria = topicExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        List<Topic> topics = topicMapper.selectByExampleWithBLOBs(topicExample);
        ListData<Topic> listData = new ListData<>();
        listData.setData(topics);
        listData.setCount((int) new PageInfo<Topic>(topics).getTotal());
        return listData;
    }

    /**
     * 在评论表中查找所有type=1的评论
     * 然后根据userId查找user信息 （nickname + avatar）,
     * @param valueId
     * @param type
     * @param showType
     * @param page
     * @param size
     * @return
     */
    @Override
    public TopicDetailWithCommentsVO list(String valueId, String type, String showType, Integer page, Integer size) {
        PageHelper.startPage(page,size);
        //Request URL: http://182.92.235.201:8083/wx/comment/list?valueId=283&type=1&showType=0&page=1&size=5
        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();
        criteria.andTypeEqualTo((byte) 1);
        criteria.andValueIdEqualTo(Integer.valueOf(valueId));
        //选出了所有评论，封装到dataBeans中
        List<Comment> comments = commentMapper.selectByExample(commentExample);
//https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif?imageView2/1/w/80/h/80
        List<TopicDetailWithCommentsVO.DataBean> dataBeans = new LinkedList<>();

        for (Comment comment : comments) {
            TopicDetailWithCommentsVO.DataBean dataBean = new TopicDetailWithCommentsVO.DataBean();
            dataBean.setAddTime(comment.getAddTime());
            dataBean.setContent(comment.getContent());
            dataBean.setPicList(Arrays.asList(comment.getPicUrls()));
            TopicDetailWithCommentsVO.DataBean.UserInfoBean userInfo = new TopicDetailWithCommentsVO.DataBean.UserInfoBean();
            Integer userId = comment.getUserId();
            User user = userMapper.selectByPrimaryKey(userId);
            userInfo.setNickName(user.getNickname());
            userInfo.setAvatarUrl(user.getAvatar());
            dataBean.setUserInfo(userInfo);
            dataBeans.add(dataBean);
        }
        TopicDetailWithCommentsVO topicDetailWithCommentsVO = new TopicDetailWithCommentsVO();
        topicDetailWithCommentsVO.setData(dataBeans);
        topicDetailWithCommentsVO.setCurrentPage(page);
        topicDetailWithCommentsVO.setCount((int) new PageInfo<Comment>(comments).getTotal());
        return topicDetailWithCommentsVO;
    }
}


















